<?php
/**
 * Crea un formulario para insertar datos en una table
*/
class helper_Form{
    public $s_method = 'post'; 
    public $s_action = '';
    public $s_enctype = 'multipart/form-data';
    public $s_request = '';
    public $a_fields = array();
    public $b_autosubmit = true;
    public $s_submittext = 'Ok';
    public $s_resettext = 'Reset';

    function __construct($s_action, $s_request, $s_method = 'post'){
		$this->s_method = $s_method;
		$this->s_action = $s_action;
		$this->s_request = $s_request;
    }
    
    function add(ABField $o_fields){
		$this->a_fields[$o_fields->s_name] = $o_fields;
		return $this;
    }

    function setSCaption($s_submit = 'submit', $s_reset ='reset'){
    	$this->s_submittext = $s_submit;
		$this->s_resettext = $s_reset;
		return $this;
    }

    function setError(){
		$a_args = is_array(func_get_arg(0))?func_get_arg(0):func_get_args();
		foreach($this->a_fields as $s_key => $o_field){
		    if(!in_array($s_key, $a_args))continue;
		    $o_field->a_class[] = 'error';
		}
    }

    function __get($s_name){
		return (array_key_exists($s_name, $this->a_fields))? $this->a_fields[$s_name] : null;
    }
}

abstract class ABField{
	public $s_name;
	public $s_label;
	public $s_desc;
	public $a_class;

	function __construct($s_name, $s_label, $s_desc, $a_class=array()){
	    $this->s_name = $s_name;
	    $this->s_desc = $s_desc;
	    $this->s_label = $s_label === null ? $s_name:$s_label;
	    $this->a_class = $a_class;
	}

	
	abstract function render();
}

class ABFText extends ABField{
    protected $i_maxlength = 30;
    protected $s_value = '';
    
    function __construct($s_name, $s_label, $s_desc, $s_value, $i_maxlength = 30,$a_class=array()){
		parent::__construct($s_name, $s_label, $s_desc, $a_class);
		$this->s_value = $s_value;
		$this->i_maxlength = $i_maxlength;
    }
	
    function render(){
		$s_class = implode(', ', $this->a_class);
		return "<p>
		<label for=\"{$this->s_name}\">{$this->s_label}:<span>{$this->s_desc}</span></label>
		<input type=\"text\" name=\"{$this->s_name}\" id =\"{$this->s_name}\" value=\"{$this->s_value}\" maxlength=\"{$this->i_maxlength}\" class=\"$s_class\"/>
		</p>";
    }
}

class ABFTArea extends ABField{
	protected $i_rows = 15;
	protected $i_cols = 30;
	protected $s_value = '';
	
	function __construct($s_name, $s_label, $s_desc, $s_value, $i_rows = 15, $i_cols =30, $a_class=array()){
		parent::__construct($s_name, $s_label, $s_desc, $a_class);
		$this->s_value = $s_value;
		$this->i_rows = $i_rows;
		$this->i_cols = $i_cols;
    }
	
	
	function render(){
		$s_class = implode(', ', $this->a_class);
		return "<p><label for=\"{$this->s_name}\">{$this->s_label}:<span>{$this->s_desc}</span></label>
		<br /><textarea rows=\"{$this->i_rows}\" cols=\"{$this->i_cols}\" name=\"{$this->s_name}\" id=\"{$this->s_name}\" class=\"$s_class\">{$this->s_value}</textarea>
		</p>";
	}
}

class ABFHidden extends ABField{
	protected $i_maxlength = 30;
	protected $s_value = '';
	
	function render(){
		$s_class = implode(', ', $this->a_class);
		return "<input type=\"hidden\" name=\"{$this->s_name}\" id =\"{$this->s_name}\" value=\"{$this->m_value}\"/>";
	}
}

class ABFCheckbox extends ABField{
	protected $i_maxlength = 30;
	protected $s_value;
	
	function __construct($s_name, $s_label, $s_desc, $b_value, $a_class=array()){
		parent::__construct($s_name, $s_label, $s_desc, $a_class);
		$this->s_value = $b_value ? 'checked="checked"':'';
    }

	function render(){
		$s_class = implode(', ', $this->a_class);
		return "<p>
		<label for=\"{$this->s_name}\">{$this->s_label}:<span>{$this->s_desc}</span></label>
		<input type=\"checkbox\" name=\"{$this->s_name}\" id =\"{$this->s_name}\" {$this->s_value} class=\"$s_class\"/>
		</p>";
	}
}

class ABFSelect extends ABField{
	protected $a_value = '';
	
	function __construct($s_name, $s_label, $s_desc, $a_value, $a_class=array()){
		parent::__construct($s_name, $s_label, $s_desc, $a_class);
		$this->a_value = $a_value;
    }
	
	function render(){
		$s_class = implode(', ', $this->a_class);
		$s_tmp = "<p><label for=\"{$this->s_name}\">{$this->s_label}:<span>{$this->s_desc}</span></label>
		<select name=\"{$this->s_name}\" id=\"{$this->s_name}\" class=\"$s_class\">";
		foreach($this->a_value as $s_key => $s_value)
			$s_tmp .= "<option value=\"$s_value\">$s_key</option>";
		$s_tmp .= '</select></p>';
		return $s_tmp;
	}
}
?>
